package com.atguigu.jxc.service.impl;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.entity.bejson.pojo.JsonRootBean;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }



    @Override
    public List<GoodsType> loadGoodsType() {
        List<ParentClass> list = goodsDao.loadGoodsType();
        GoodsType goods = new GoodsType();
        List<GoodsType>list1 = new ArrayList<>();
        for (ParentClass p:list
             ) {
            goods.setGoodsTypeId(p.getGoodsTypeId());
            goods.setGoodsTypeName(p.getGoodsTypeName());
            goods.setGoodsTypeState(p.getGoodsTypeState());
            list1.add(goods);
            List<SubClass> subClassList = p.getSubClassList();
            for (SubClass s:subClassList
                 ) {
                goods.setGoodsTypeId(s.getGoodsTypeId());
                goods.setGoodsTypeName(s.getGoodsTypeName());
                goods.setGoodsTypeState(s.getGoodsTypeState());
                list1.add(goods);
            }
        }
        System.out.println(list);
        return list1;
    }

    @Override
    public List<Goods> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
            ArrayList<Goods> goods = (ArrayList<Goods>) goodsDao.listInventory(page-1,rows);
            return goods;
    }

    @Override
    public JsonRootBean loadGoodsType2() {
        return null;
    }

    /**
     * 分页 + 模糊查询 商品 上面的方法废弃 懒得删除
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @Override
    public List<Goods> list(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        List<Goods> goods = goodsDao.list(page,rows,codeOrName,goodsTypeId);
        return goods;
    }


}
